package controller;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import BizLogic.BookingManager;

import model.BookingDTO;

import dao.BookingDAO;
import dao.DAOFactory;
import dao.FacilityTypeDAO;

/**
 * Servlet implementation class ViewBookingServlet
 */
@WebServlet("/ViewBookingServlet")
public class ViewBookingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewBookingServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	doProcess(request,response);	
	}

	
	private void doProcess(HttpServletRequest request,
			HttpServletResponse response)throws ServletException,IOException {
		// TODO Auto-generated method stub
		try
		{
			if(request.getParameter("Update")!=null)
			if(request.getParameter("Update").equals("Update"))
			{
				BookingManager manager=new BookingManager();
				BookingDTO book=new BookingDTO();
				book.setBookingID(Integer.parseInt(request.getParameter("bookingID")));
				book.setFacilityName(request.getParameter("facilityName"));
				book.setFromDate(Date.valueOf(request.getParameter("fromDate")));
				book.setPriorityID(Integer.parseInt(request.getParameter("priority")));
				book.setReason(request.getParameter("reason"));
				book.setStaffID(request.getParameter("staffID"));
				book.setToDate(Date.valueOf(request.getParameter("toDate")));
				book.setStatus(request.getParameter("status"));
				if(!(manager.detectOverLap(book.getFromDate(), book.getToDate(), book.getFacilityName())))
				{
					manager.updateRecord(book);
				}
				
			}
			HttpSession session=request.getSession();
	
			
			ArrayList<BookingDTO> book=new BookingManager().getBookingDetails(session.getAttribute("StaffID").toString());
//			BookingDTO dto=new BookingDTO();
			RequestDispatcher rd=request.getRequestDispatcher("ViewBookingStatus.jsp");
			request.setAttribute("book", book);
			rd.forward(request, response);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request,response);
	}

}
